function layout = aft_Layout(hfile)
% AFT::Layout  - get layout signature
%
% FORMAT:       layout = obj.Layout;
%
% No input fields
%
% Output fields:
%
%       layout      1x14 vector representing the layout of the dataset
%        (1:3)      XYZ-dim ([NrOfVertices, 1, 1] for MTC/SRF)
%        (4)        T-dim (1 for 3D datasets)
%        (5:7)      XYZ-start values ([1, 1, 1] for FMR, [0, 0, 0] for SRF)
%        (8:10)     XYZ-End values ((1:3) for FMR, [256, 256, 256] for SRF)
%        (11:13)    spatial resolution ([1, 1, 1] for SRF/MTC)
%        (14)       temporal resolution (1 for 3D datasets)
%        (15)       type signature
%
% TYPES: AMR, AVA, CMP, DDT, DMR, FMR, GLM, MAP, MTC, MSK, SRF, VDW, VMP, VMR, VTC
%
% Note: output is in BV's *internal* notation (axes not in TAL order!)
%       also, this function is only valid for native BrainVoyager formats
%       with at least either temporal or spatial information!

% Version:  v0.8a
% Build:    9102118
% Date:     Oct-21 2009, 6:20 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, true)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end

% create default layout
layout = ones(1, 15);
layout(8:10) = 256;

% switch on filetype
sc = bvqxfile_getscont(hfile.L);
bc = sc.C;
ft = lower(sc.S.Extensions{1});
layout(end) = sum(double(ft) .* (2 .^ ((8 * numel(ft) - 1):-8:0)));
try
    switch (ft)
        case {'amr'}
            try
                layout(1:2) = size(bc.Slice(1).AMRData);
            catch
                layout(1:2) = 256;
            end
            layout(3) = numel(bc.Slice);
            layout(8:10) = layout(5:7) + layout(1:3) - 1;
        case {'ava', 'cmp', 'ddt', 'glm', 'msk', 'vdw', 'vmp', 'vtc'}
            if strcmp(ft, 'vtc')
                layout(4) = bc.NrOfVolumes;
                layout(14) = 0.001 * bc.TR;
            end
            layout(5:10) = [bc.XStart, bc.YStart, bc.ZStart, ...
                bc.XEnd, bc.YEnd, bc.ZEnd];
            layout(11:13) = bc.Resolution;
            try
                vol = aft_GetVolume(hfile, 1);
                layout(1:3) = size(vol);
            catch
                layout(1:3) = floor((layout(8:10) - layout(5:7)) / bc.Resolution); 
            end
        case {'ctc'}
            layout(1) = bc.NrOfChannels;
            layout(4) = bc.NrOfSamples;
            layout(14) = 1 / bc.SamplingFrequency;
        case {'dmr', 'fmr'}
            layout(1:3) = [bc.ResolutionX, bc.ResolutionY, bc.NrOfSlices];
            layout(4) = bc.NrOfVolumes;
            layout(8:10) = layout(5:7) + layout(1:3) - 1;
            layout(11:12) = [bc.InplaneResolutionX, bc.InplaneResolutionY];
            layout(13) = bc.SliceThickness + bc.GapThickness;
            layout(14) = 0.001 * bc.TR;
        case {'eig'}
            layout(4) = numel(bc.EigenValues);
        case {'map'}
            try
                layout(1:2) = size(bc.Map(1).Data);
            catch
                layout(1:2) = [bc.DimX, bc.DimY];
            end
            layout(3) = numel(bc.Map);
            layout(8:10) = layout(5:7) + layout(1:3) - 1;
        case {'mtc'}
            layout(1) = size(bc.MTCData, 2);
            layout(4) = size(bc.MTCData, 1);
            layout(5:7) = 0;
            layout(14) = 0.001 * bc.TR;
        case {'srf'}
            layout(1) = size(bc.VertexCoordinate, 1);
            layout(5:7) = 0;
        case {'vmr'}
            try
                layout(1:3) = size(bc.VMRData);
            catch
                layout(1:3) = 0;
            end
            layout(5:7) = [bc.OffsetX, bc.OffsetY, bc.OffsetZ];
            layout(8:10) = layout(5:7) + layout(1:3) - 1;
            layout(11:13) = [bc.VoxResX, bc.VoxResY, bc.VoxResZ];
        otherwise
            error('BAD_FILETYPE');
    end
catch
    error( ...
        'BVQXfile:InternalError', ...
        'Layout is not a valid method of type %s.', ...
        upper(ft) ...
    );
end
